// pages/my/myTopics.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topics: [],
    page_cnt: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.onGetMyTopics();
  },

  onReachBottom: function () {
    var that = this;
    var page_cnt = that.data.page_cnt + 10;
    this.setData({
      page_cnt: page_cnt
    })
    that.onGetMyTopics();
  },

  //得到我的发布帖子
  onGetMyTopics: function(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    wx.request({
      url: app.globalData.targetUrl + 'history/topic/',
      data: {
        user_id: user_id,
        is_publish: 1,
        now_page: 1,
        page_cnt: that.data.page_cnt
      },
      success: res => {
        that.setData({
          topics: res.data.data
        })
        console.log(res.data);
      }
    })
  },

  //点击跳转帖子详情
  onTopicTap: function(e){
    var topic_id = e.currentTarget.dataset.topic_id;
    wx.navigateTo({
      url: '/pages/lifeCircle/comments?topic_id=' + topic_id,
    })
  },
  

  //滑动删除
   touchStart: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      touchStartPageX: e.changedTouches[0].pageX,
    })
  },

  touchEnd: function (e) {
    var index = e.currentTarget.dataset.index;
    let touchEndPageX = e.changedTouches[0].pageX;
    var left = "scrollLeft[" + index + "]";
    var offSetStartToEnd = touchEndPageX - this.data.touchStartPageX;
    if (offSetStartToEnd < 10 & offSetStartToEnd > -10) {
      return;
    };
    if (offSetStartToEnd > 10) {
      if (this.data.scrollLeft === 0) return;
      this.setData({
        [left]: 0
      });
    };
    if (offSetStartToEnd < -50) {
      this.setData({
        [left]: 60
      })
    }
  },
  deleteTopic: function (e) {
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var topic_id = e.currentTarget.dataset.topic_id;
    console.log("user_id=" + user_id + "topic_id=" + topic_id);
    wx.request({
      url: app.globalData.targetUrl + 'discuss/topics/',
      //method: 'DELETE',
      data: {
        user_id: user_id,
        topic_id: topic_id,
        is_delete: 1
      },
      success: res => {
        console.log(res.data);
        that.setData({
          topics: res.data.data
        })
        wx.showToast({
          title: '删除成功',
        })
        that.onGetMyTopics();
      }
    })
  }
})